﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ElectronicCommerce.Framework.Collections;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.ServiceModel.Configuration
{
    /// <summary>
    /// 服务配置元素。
    /// </summary>
    public class ServiceElement : IKeyedObject
    {
        #region [ Fields ]

        private string name;
        private string serviceTypeAssemblyName;
        private string serviceTypeName;
        private string classTypeName;
        private ServiceCallType callType;
        private string server;
        private string binding;
        private string relativeAddress;

        #endregion

        #region [ Properties ]

        /// <summary>
        /// 键值。
        /// </summary>
        public string Key
        {
            get { return GenerateKey(this.serviceTypeName, this.name); }
        }

        /// <summary>
        /// 名称
        /// </summary>
        [XmlAttribute("name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 服务接口名。
        /// </summary>
        [XmlAttribute("serviceType")]
        public string ServiceTypeAssemblyName
        {
            get { return serviceTypeName; }
            set
            {
                serviceTypeAssemblyName = value;
                serviceTypeName = TypeHelper.GetTypeName(value);
            }
        }

        /// <summary>
        /// 服务接口名。
        /// </summary>
        public string ServiceTypeName
        {
            get { return this.serviceTypeName; }
        }

        /// <summary>
        /// 服务实现
        /// </summary>
        [XmlAttribute("classType")]
        public string ClassTypeName
        {
            get { return classTypeName; }
            set { classTypeName = value; }
        }

        /// <summary>
        /// 调用类型
        /// </summary>
        [XmlAttribute("callType")]
        public ServiceCallType CallType
        {
            get { return callType; }
            set { callType = value; }
        }

        /// <summary>
        /// 服务器。仅当WCF调用时有效。
        /// </summary>
        [XmlElement("server")]
        public string ServerName
        {
            get { return server; }
            set { server = value; }
        }

        /// <summary>
        /// WCF绑定配置。仅当WCF调用时有效。
        /// </summary>
        [XmlElement("binding")]
        public string BindingName
        {
            get { return binding; }
            set { binding = value; }
        }

        /// <summary>
        /// WCF服务相对地址。仅当WCF调用时有效。
        /// </summary>
        [XmlElement("relativeAddress")]
        public string RelativeAddress
        {
            get { return relativeAddress; }
            set { relativeAddress = value; }
        }

        /// <summary>
        /// WCF服务地址信息。
        /// </summary>
        public ServiceServerElement Server
        {
            get
            {
                return ServiceModelConfigHelper.ServiceServerConfig.Servers[ServerName];
            }
        }

        /// <summary>
        /// WCF服务绑定信息。
        /// </summary>
        public ServiceBindingElement Binding
        {
            get
            {
                return ServiceModelConfigHelper.ServiceBindingConfig.Bindings[BindingName];
            }
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// 集成服务配置键值。
        /// </summary>
        /// <param name="serviceTypeName">服务类型名称。</param>
        /// <param name="serviceName">服务名称。</param>
        /// <returns>键值。</returns>
        public static string GenerateKey(string serviceTypeName, string serviceName)
        {
            return string.Format("{0}_{1}", serviceTypeName, serviceName);
        }

        #endregion
    }
}
